<div id="content">
  <div id="content-header">
    <div id="breadcrumb"> 
        <a href="#" class="tip-bottom" data-original-title="Go to Home"><i class="icon-home"></i> 首页</a> 
        <a href="#" class="tip-bottom" data-original-title="">GM管理</a> 
        <a href="#" class="current">GM账号管理</a> 
    </div>
   <!--  <h1>Tables</h1> -->
  </div>
  <div class="container-fluid">
   <!--  <hr> -->
    <div class="row-fluid">
      <div class="span12">
        <div class="row-fluid">
          <form class="form-search">
            用户uid ：<input id="uid" type="text" class="input-medium search-query" placeholder="用户uid" >
            &nbsp;&nbsp;用户名 ：<input id="username" type="text" class="input-medium search-query" placeholder="用户名" >
            <button id="usersearch" type="button" class="btn  btn-success">查&nbsp;询</button>

            <!-- <button id="btnAdd" class="btn btn-success pull-right">添&nbsp;加</button> -->
          </form>
            
            
        </div>
        <div class="widget-box">
          <div class="widget-title"> <span class="icon"> <i class="icon-th"></i> </span>
            <h5>用户列表</h5>
          </div>
          <div class="widget-content nopadding">
            <table id="dataList" class="table table-bordered table-striped">
              <thead>
                <tr>
                  <th>用户uid</th>
                  <th>用户账号</th>
                  <th>账号类型</th>
                  <th>注册时间</th>
                  <th>操作</th>
                </tr>
              </thead>
              <tbody>
                
              </tbody>
            </table>
          </div>
        </div>

        <div class="row-fluid">
            <div id="pagination" class="pagination alternate pull-right">
            </div>
        </div>

        </div>
      </div>
    </div>
  </div>
</div>

<div id="dialog-form" title="Create new user" style=" display: none;">
  <p class="validateTips"></p>
  <div id="userinfoedit">
   <form action="#" method="get" class="form-horizontal">
      <input type="hidden" id="d_uid"> 

      <table >
        <tr>
          <td>
            <div class="control-group">
              <label class="control-label">修改密码 :</label>
              <div class="controls">
                <input id="password" type="text" class="span11" placeholder="">
              </div>
            </div>
          </td>
          <td>
            <div class="control-group">
              <label class="control-label">修改账号类型 :</label>
              <div class="controls">
                <input id="roleId" type="text" class="span11">
                <span class="help-block"></span>
              </div>
            </div>
          </td>
        </tr>

      </table>  
    </form>
  </div>
</div>

<script type="text/javascript">

  var search=document.getElementById('usersearch');

  utils.addEvent(search,'click',searchCallback);

  function searchCallback(){
    /*var uid=document.getElementById('uid'),
    username=document.getElementById('username'),
    data={};

    data.uid=uid.value;
    data.username=username.value;
    
    $.ajax({
          type:"post",
          data:data,
          url:"/gmmgr/search",
          dataType:'json',
          success:function(res){
            var data= res.data;
            var table=document.getElementById("dataList");
            removeAllRow(table);
            for(var i=0;i<data.length;i++){
              addRow(table,'',data[i]);
            }
          }
    });*/
  }

  $(function(){
    // get data
    getDataList();
  });

  function getDataList(index){
    var data={};
    data.page=index||0;
    data.numPerPage=pagesize;
    $.ajax({
      type:"post",
      url:"/gmmgr/getUser",
      data:data,
      dataType:'json',   
      success:function(res){
        
        var data=res.data;
        var table=document.getElementById("dataList");
        var tbody= table.getElementsByTagName('tbody')[0];

        dataTable.clearData(tbody);
        
        for(var i=0;i<data.list.length;i++){
          addRow(tbody,'',data.list[i]);
        }
        
        if(!index||index==0){
          initPagination(data.count);
        }

      }
    });
  }

  function initPagination(page) {
    $("#pagination").pagination(page, {
        callback: pageselectCallback,
        items_per_page:pagesize, // 每页只显示一条记录
        num_display_entries : 3,
        num_edge_entries : 1, 
        prev_text : "上一页",  
        next_text : "下一页", 
    });
  }

  function pageselectCallback(page_index, jq){
    getDataList(page_index);
    return false;
  }

  function addRow(table,index,data){
    var row=table.insertRow();
    var cell=row.insertCell(0);
    var cell1=row.insertCell(1);
    var cell2=row.insertCell(2);
    var cell3=row.insertCell(3);
    var cell4=row.insertCell(4);

    cell.innerHTML=data.uid;
    cell1.innerHTML=data.userName;
    cell2.innerHTML=data.roleId;
    cell3.innerHTML=new Date(data.createTime*1000).format("yyyy-MM-dd HH:mm:ss");

    var a=document.createElement('a');
    a.innerHTML="&nbsp;编辑&nbsp;";
    a.onclick=function(){userEdit(data.uid);}
    cell4.appendChild(a);

    var b=document.createElement('a');
    b.innerHTML="删除";
    //b.data=data.uid;
    b.onclick=function(){userDel(data.uid);}
    cell4.appendChild(b);
  }

  function userDel(uid){
    $.ajax({
          type:"post",
          url:"/gmmgr/delUser/"+uid,
          dataType:'json',
          success:function(res){
            var data= res.data;

            if (data.code != 0) {
              alert(data.info);
            }
            else {
              alert('操作成功');
              location.reload();
            }
          }
        });
  }

  function userEdit(id){
  var url="/gmmgr/getUserInfo/"+id;
  
   $( "#dialog-form" ).dialog({
        width: 700,
        title:'修改用户信息',
        modal:true,
        closeOnEscape:true,
        resizable:false,
        open:function(){
          var data={};
          data.action='';
          initialization();
          $.ajax({
            type:"post",
            data:'data',
            url:url,
            dataType:'json',
            success:function(res){
            var data= res.data;
            var password=document.getElementById('password'),
                roleId=document.getElementById('roleId');

                password.value=data.password;
                roleId.value=data.roleId;
            }
          });
        },
        buttons:{
          "修改":function(){

            var password=document.getElementById('password'),
                roleId=document.getElementById('roleId'),
                
                data={};

                data.password=password.value;
                data.roleId=roleId.value;

              $.ajax({
                type:"post",
                url:"/gmmgr/updateUserInfo/"+id,
                data:data,
                dataType:'json',
                success:function(res){
                  alert(res.info);
                  if(res.code==0){

                    location.reload();

                  }else{
                    
                  }
                }
              });
          },
          "取消":function(){
            $(this).dialog("close");
          }

        }
   });

}

</script>